﻿using Dapper;
using Gx.Common.Core;
using Gx.Model.Entity;
using Gx.Model.ViewModel;
using System.Collections.Generic;
using System.Linq;
namespace Gx.Dal.Manage
{
    public class LKIncidentalsDal : BaseDal<LKIncidentals>
    {

        /// <summary>
        /// 名称是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool AnyName(string name)
        {
            using (var conn = new DbContext())
            {
                var total = conn.Connection.QueryFirst<int>("SELECT COUNT(1) FROM LK_Incidentals WHERE IsDeleted=0 AND Name=@name", new { name });
                return total > 0;
            }
        }

        /// <summary>
        /// 获取最大编号
        /// </summary>
        /// <returns></returns>
        public string GetMaxLKIncidentalsCode()
        {
            using (var conn = new DbContext())
            {
                return conn.Connection.QueryFirst<string>("SELECT MAX(LKIncidentalsCode) FROM LK_Incidentals");
            }
        }

        /// <summary>
        /// 获取合同下的附加费用
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public List<LKIncidentals> GetLKIncidentalsListByContract(int id, int type)
        {
            using (var conn = new DbContext())
            {
                return conn.Connection.Query<LKIncidentals>("SELECT * FROM LK_Incidentals WHERE ContractId=@id AND ContractType=@type AND IsDeleted=0", new { id, type }).ToList();
            }
        }

        ///// <summary>
        ///// 分页查询
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //public PagedList<LKIncidentals> GetLKIncidentalsPageList(LKIncidentalsInput input)
        //{
        //    string sql = "SELECT * FROM LK_Incidentals WHERE IsDeleted=0 @WHERE";
        //    List<string> conditions = new List<string>();
        //    //判断input的属性是否为空，如果不为空则添加到conditions中

        //    string condition = string.Empty;
        //    if (conditions.Any())
        //    {
        //        condition = $" AND {string.Join(" AND ", conditions)}";
        //    }
        //    sql = !string.IsNullOrWhiteSpace(condition) ? sql.Replace("@WHERE", condition) : sql.Replace("@WHERE", "");

        //    using (var conn = new DbContext())
        //    {
        //        return conn.Connection.GetPagedList<LKIncidentals>(sql, "CreateTime Desc", input);
        //    }
        //}
    }
}

